package vista.ventanas;


import javax.swing.JLabel;

import vista.paneles.PanelParLabelText;

import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.GridBagLayout;
import java.awt.Window;

import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

@SuppressWarnings("serial")
public class VentanaProyectoNuevo extends SubVentana {

	private final static int ALTO_VENTANA = 250;
	
	private PanelParLabelText nombreProyecto;
	private JTextField textPath;
	private JButton boton;

	public VentanaProyectoNuevo(Window frame) 
	{
		super(frame, "Crear Proyecto Nuevo", ALTO_VENTANA);
		
		this.escucharEventoBuscarPath();
	}

	public String getPath() {
		return textPath.getText();
	}
	
	public String getNombreProyecto(){
		return this.nombreProyecto.getInformacion();
	}
	
	@Override
	protected void agregarComponentesPanelInformacion()
	{
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, 1.0, 1.0, Double.MIN_VALUE};
		
		getPanelDeInformacion().setLayout(gridBagLayout);
		
		agregarPanelNombre();
		agregarLabelPath();
		agregarTxtPath();
		agregarBotonBuscarPath();
	}

	private void agregarPanelNombre() 
	{
		this.nombreProyecto = new PanelParLabelText("Nombre:");
		
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(0, 0, 5, 5);
		gbc.gridwidth = 2;
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		
		getPanelDeInformacion().add(nombreProyecto, gbc);
	}
	
	private void agregarBotonBuscarPath()
	{
		boton = new JButton("");
		boton.setIcon(new ImageIcon(getClass().getResource("/vista/imagenes/abrirProyecto32.png")));
		
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.gridx = 1;
		gbc_btnNewButton.gridy = 2;
		
		getPanelDeInformacion().add(boton, gbc_btnNewButton);
	}

	private void escucharEventoBuscarPath() 
	{
		boton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) 
			{
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				
				int returnVal = chooser.showOpenDialog(null);
				if(returnVal == JFileChooser.APPROVE_OPTION)
					textPath.setText(chooser.getSelectedFile().toString());
			}
		});
	}

	private void agregarTxtPath() 
	{
		textPath = new JTextField();
		textPath.setEditable(false);
		
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.insets = new Insets(0, 0, 0, 5);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 0;
		gbc_textField.gridy = 2;
		
		getPanelDeInformacion().add(textPath, gbc_textField);
	}

	private void agregarLabelPath() 
	{
		JLabel lblPath = new JLabel("Path");
		
		GridBagConstraints gbc_lblPath = new GridBagConstraints();
		gbc_lblPath.anchor = GridBagConstraints.SOUTHWEST;
		gbc_lblPath.gridwidth = 2;
		gbc_lblPath.insets = new Insets(0, 0, 5, 5);
		gbc_lblPath.gridx = 0;
		gbc_lblPath.gridy = 1;
		
		getPanelDeInformacion().add(lblPath, gbc_lblPath);
	}
}
